import { CHANNEL, CVERSION, PROJECTNAME } from "../globalData";
import { showToast } from "../handlers";

const globalData = getApp().globalData;

function postUpload() {
    wx.uploadFile({
        header: {
            sessionId: globalData.loginInfo.sessionId
        },
        url: `${PROJECTNAME}/restaurant-user/file/upload`,
        name: 'file',
        filePath: setting.filePath,
        formData: {
            cversion: CVERSION,
            channel: CHANNEL,
            smartUserId: globalData.loginInfo.loginedUser.userId,
            storagePath: setting.storagePath
        },
        success: res => {
            const { data = { code: '500', msg: 'data is no define edited by Dom.' } } = res;
            const result = typeof data === 'string' ? JSON.parse(data) : data;
            const { code, msg } = result;
            if (code === 6666) {
                if (typeof setting.uploadSuccess === 'function') {
                    setting.uploadSuccess(result.data.uploadedList)
                } else {
                    showToast({ title: 'setting.uploadSuccess show be a function' })
                }
            } else {
                showToast({ title: msg })
            }
        },
        fail: res => {
            showToast({
                title: '文件上传失败'
            })
        }
    })
}

module.exports = {
    postUpload
};